﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using Bitis_KeToanThue_Pro_QuanTri.AccessDatabase;
using Bitis_KeToanThue_Pro_DungChung.Common;
using System.IO;

namespace Bitis_KeToanThue_Pro_QuanTri.Form
{
    public partial class frmDanhMucChuongTrinh : Telerik.WinControls.UI.RadForm
    {
        DanhMucChuongTrinh dm = new DanhMucChuongTrinh();
        public static int IDCu = 0;
        public static string option = "";
        public frmDanhMucChuongTrinh()
        {
            InitializeComponent();
            khoitaogrid();
            loadgrid();
            ClassXuLyChung.loadcombodungchung(cbNhomChuongTrinh, dm.loadcombonhomchuongtrinh(), "DienGiai", "NhomCT");
            trangthaisudungcontrol(false);
            trangthaisudung2button(true);
            trangthaisudung3button(false);
            UpdatePanelInfo(this.radGridView1.CurrentRow);
            if (BienHeThong.ht_quyencapnhat == 1)
            {
                btnSua.Enabled = true;
                btnThem.Enabled = true;
            }
            else
            {
                btnSua.Enabled = false;
                btnThem.Enabled = false;
            }
            if (BienHeThong.ht_quyenxoa == 1)
            {
                btnXoa.Enabled = true;
            }
            else
            {
                btnXoa.Enabled = false;
            }
        }
        private void trangthaisudung2button(bool trangthaidung)
        {
            btnThem.Enabled = trangthaidung;
            btnSua.Enabled = trangthaidung;
        }
        private void trangthaisudung1button(bool trangthaidung)
        {
            btnXoa.Enabled = trangthaidung;

        }
        private void trangthaisudung3button(bool trangthaidung)
        {
            btnLuu.Enabled = trangthaidung;
            btnKhong.Enabled = trangthaidung;
            btnSoanLai.Enabled = trangthaidung;
        }
        private void trangthaisudungcontrol(bool trangthaidung)
        {
            txtChuongTrinh.Enabled = trangthaidung;
            txtDienGiai.Enabled = trangthaidung;

          
            cbNhomChuongTrinh.Enabled= trangthaidung;
            
            btnChonHinh.Enabled = trangthaidung;

            txtChuongTrinh.Focus();
        }
        public void loadgrid()
        {
            this.radGridView1.DataSource = dm.loadgird();
        }
        public void khoitaogrid()
        {
            this.radGridView1.EnableHotTracking = true;
            this.radGridView1.ShowFilteringRow = false;
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView1.MasterTemplate.ShowFilteringRow = false;
            this.radGridView1.MasterTemplate.AutoExpandGroups = true;
            this.radGridView1.TableElement.RowHeight = 32;
            this.radGridView1.MasterTemplate.AllowAddNewRow = false;
            this.radGridView1.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView1.MasterTemplate.AllowEditRow = true;
            this.radGridView1.MasterTemplate.AllowDeleteRow = false;
            this.radGridView1.MasterTemplate.EnableSorting = true;
            this.radGridView1.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView1.AutoScroll = true;
            this.radGridView1.EnableAlternatingRowColor = true;
           

            GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn() { HeaderText = "", Name = "Chon", Width = 20, IsVisible = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(checkBoxColumn);

            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn() { FieldName = "MaChuongTrinh", Name = "MaChuongTrinh", HeaderText = "ID", Width = 50, IsVisible = true, ReadOnly = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(textBoxColumn);


            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn() { FieldName = "TenChuongTrinh", Name = "TenChuongTrinh", HeaderText = "Tên Chương Trình", Width = 225, IsVisible = true, ReadOnly = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(textBoxColumn1);


            GridViewTextBoxColumn textBoxColumn2 = new GridViewTextBoxColumn() { FieldName = "DienGiai", Name = "DienGiai", HeaderText = "Diễn Giải", Width = 225, IsVisible = true, ReadOnly = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(textBoxColumn2);

            GridViewTextBoxColumn textBoxColumn3 = new GridViewTextBoxColumn() { FieldName = "NhomCT", Name = "NhomCT", HeaderText = "NhomCT", Width = 225, IsVisible = false, ReadOnly = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(textBoxColumn3);
            GridViewTextBoxColumn textBoxColumn4 = new GridViewTextBoxColumn() { FieldName = "TenNhom", Name = "TenNhom", HeaderText = "Nhóm Chương Trình", Width = 225, IsVisible = true, ReadOnly = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(textBoxColumn4);
            GridViewImageColumn imagecolumn = new GridViewImageColumn()
            {
                FieldName = "Hinh",
                Name = "Hinh",
                HeaderText = "Icon",
                Width = 32,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter,
                ImageLayout = ImageLayout.Stretch,
                ImageAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(imagecolumn);

        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        public void SoanLai()
        {
            txtChuongTrinh.Text = "";
           txtDienGiai.Text = "";
            txtTimKiem.Text = "";
            pictureBoxIcon.Image = null;
         
        }
        private void btnSoanLai_Click(object sender, EventArgs e)
        {
            SoanLai();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            option = "Sua";
            trangthaisudungcontrol(true);
            trangthaisudungluoi(true);
            trangthaisudung2button(false);
            trangthaisudung1button(false);
            trangthaisudung3button(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            DialogResult kq = RadMessageBox.Show("Bạn có muốn chắc xóa các Chương Trình : được chọn không ???", "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
            if (kq == DialogResult.Yes)
            {
                for (int i = 0; i < radGridView1.Rows.Count; i++)
                {
                    if (radGridView1.Rows[i].Cells[0].Value != null)
                    {
                        bool isChecked = (bool)radGridView1.Rows[i].Cells[0].Value;
                        if (isChecked == true)
                        {
                            dm.xoa(ClassXuLyChung.GetSafeInt(radGridView1.Rows[i].Cells[1].Value));

                        }
                    }
                }
                RadMessageBox.Show("Xóa Thành Công", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                SoanLai();
                loadgrid();
                UpdatePanelInfo(this.radGridView1.CurrentRow);
            }
        }
        public void trangthaisudungluoi(bool trangthaidung)
        {
            radGridView1.Enabled = trangthaidung;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            option = "Them";
            SoanLai();
            trangthaisudungcontrol(true);
            trangthaisudungluoi(false);
            trangthaisudung2button(false);
            trangthaisudung1button(false);
            trangthaisudung3button(true);

        }

        private void radGridView1_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {

            UpdatePanelInfo(this.radGridView1.CurrentRow);
           
        }
        private void UpdatePanelInfo(GridViewRowInfo currentRow)
        {
            if (currentRow != null && !(currentRow is GridViewNewRowInfo))
            {
               IDCu = ClassXuLyChung.GetSafeInt(currentRow.Cells[1].Value);
               txtDienGiai.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[3].Value);
               txtChuongTrinh.Text = ClassXuLyChung.GetSafeString(currentRow.Cells[2].Value);
               cbNhomChuongTrinh.SelectedValue = ClassXuLyChung.GetSafeInt(currentRow.Cells[4].Value);
               DataTable dt = dm.loaddulieubyid(IDCu);

               object photoValue = dt.Rows[0]["Hinh"];
               if (!Convert.IsDBNull(photoValue) && photoValue != null)
               {
                   pictureBoxIcon.Image = ClassXuLyChung.ConvertByeToImage((byte[])photoValue);//set image property of the picture box by creating a image from stream 
                   pictureBoxIcon.SizeMode = PictureBoxSizeMode.StretchImage;//set size mode property of the picture box to stretch 
                   pictureBoxIcon.Refresh();//refresh picture box
               }
               else
               {
                   pictureBoxIcon.Image = null;
               }

            }
            else
            {
               SoanLai();
               IDCu =0;
            }
        }
        
   

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {

            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiem, radGridView1, 1, 0);
        }
        private void openfile()
        {
            OpenFileDialog OPF = new OpenFileDialog()
            {
                Title = "Open File Image",
                Filter = "Image File (.JPG,.PNG,.ICO)|*.jpg;*.png;*.ico"
            };
            if (OPF.ShowDialog() == DialogResult.OK)
            {
                txtDuongDan.Text = Path.GetFullPath(OPF.FileName).Trim();

            }
        }
        private void btnChonHinh_Click(object sender, EventArgs e)
        {
            openfile();
            if (txtDuongDan.Text != "")
            {
                pictureBoxIcon.Image = Image.FromFile(txtDuongDan.Text);
                pictureBoxIcon.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBoxIcon.Refresh();
            }
            else
            {
                pictureBoxIcon.Image = null;
            }
        }

        private void btnKhong_Click(object sender, EventArgs e)
        {
            option = "";
            trangthaisudungcontrol(false);
            trangthaisudungluoi(true);
            trangthaisudung2button(true);
            trangthaisudung1button(true);
            trangthaisudung3button(false);
            loadgrid();
            UpdatePanelInfo(this.radGridView1.CurrentRow);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtChuongTrinh.Text != "")
            {
                if (option == "Them")
                {
                    if (dm.them(txtChuongTrinh.Text,txtDienGiai.Text,ClassXuLyChung.GetSafeInt(cbNhomChuongTrinh.SelectedValue),txtDuongDan.Text) == 1)
                    {
                        RadMessageBox.Show("Thêm Thành Công Chương Trình : " + txtChuongTrinh.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                    }
                    else
                    {
                        RadMessageBox.Show(String.Format("Chương Trình: {0} đã tồn tại", txtChuongTrinh.Text), "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                    }
                }
                else
                {
                    if (option == "Sua")
                    {
                        if (IDCu != 0)
                        {
                            

                            if ( dm.sua(IDCu, txtChuongTrinh.Text,txtDienGiai.Text, ClassXuLyChung.GetSafeInt(cbNhomChuongTrinh.SelectedValue), txtDuongDan.Text)==1)
                            {
                                RadMessageBox.Show("Cập Nhật Thành Công Chương Trình : " + txtChuongTrinh.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                            }
                            else
                            {
                                RadMessageBox.Show("Chương Trình này đã tồn tại", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                            }
                        }
                        else
                        {
                            RadMessageBox.Show("Không tồn tại dữ liệu ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                        }
                    }
                }
                option = "";
                trangthaisudungcontrol(false);
                trangthaisudungluoi(true);
                trangthaisudung2button(true);
                trangthaisudung1button(true);
                trangthaisudung3button(false);
                SoanLai();
                loadgrid();
                UpdatePanelInfo(this.radGridView1.CurrentRow);

            }
            else
            {
                RadMessageBox.Show("Chưa Nhập Đầy Đủ Thông Tin. ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }
        }

  
    }
}
